# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 8: Differences in candidate evaluation by party (Appendix)
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(cjoint)
library(ggthemes)

# Load relevant functions -------------------------------------------------

source("functions/specify_parameters.R")
source("functions/estimate_AMCEs.R")
source("functions/compare_AMCEs.R")
source("functions/order_attributes_levels.R")
source("functions/set_my_ggtheme.R")
source("functions/plot_AMCEs.R")
source("functions/specify_color_palette.R")
source("functions/plot_AMCEs_comparison.R")
source("functions/compare_AMCEs_no_N_labels.R")

# Load data ---------------------------------------------------------------

load("temp/data_for_analysis_donors.RData")
load("temp/data_for_analysis_public.RData")

# Specify parameters ------------------------------------------------------

parameters <- list(list(), list(), list())

w_race <- c(0.6, 0.2, 0.2)

# Selection of baselines

parameters <- specify_parameters(df_cleaned_donors, "Gender", "Male", 1)
parameters <- specify_parameters(df_cleaned_donors, "Race", "White", 0, w_race)
parameters <- specify_parameters(df_cleaned_donors, "Discrimination", "Not a big problem", 1)
parameters <- specify_parameters(df_cleaned_donors, "Taxes", "Less progressive", 1) 
parameters <- specify_parameters(df_cleaned_donors, "Courts", "Obey courts", 1)
parameters <- specify_parameters(df_cleaned_donors, "Investigations", "Independent", 1)
parameters <- specify_parameters(df_cleaned_donors, "Compromise", "Supports compromise", 1) 
parameters <- specify_parameters(df_cleaned_donors, "Voting", "Opposes voter ID laws", 1)

constraint_list <- list()

# Create model 

model <- formula(selected ~ 
                   `Gender` +
                   `Race` +
                   `Discrimination` +
                   `Taxes` +
                   `Courts` +
                   `Investigations` +
                   `Compromise` +
                   `Voting`)

# Set the order of attributes and levels ----------------------------------

Attribute_order <- c("Gender",
                     "Race",
                     "Discrimination",
                     "Taxes",
                     "Courts",
                     "Investigations",
                     "Compromise",
                     "Voting")

Level_order <- c(c("Male",
                   "Female"),
                 c("White",
                   "Black",
                   "Hispanic"),
                 c("Not a big problem",
                   "Prevent discrimination"),
                 c("Less progressive",
                   "More progressive"),
                 c("Obey courts",
                   "Disregard politicized decisions"),
                 c("Independent", 
                   "Partisan involvement"),
                 c("Supports compromise", 
                   "Stand up to other party"),
                 c("Opposes voter ID laws", 
                   "Supports voter ID laws"))

# Estimate ACIEs ----------------------------------------------------------

# Choose data for analysis

df.republican.donors <- df_cleaned_donors %>% 
  filter(Partisanship == "Republican") %>% 
  select(-Partisanship) %>% 
  filter(!is.na(selected))

df.democrat.donors <- df_cleaned_donors %>% 
  filter(Partisanship == "Democrat") %>% 
  select(-Partisanship) %>% 
  filter(!is.na(selected))

default_colour_palette <- estimate_AMCEs("ACIEs (Donors, March 2019)", df.republican.donors, model) %>%
  specify_color_palette(length(Attribute_order))

df.republican.public <- df_cleaned_public %>% 
  filter(Partisanship == "Republican") %>% 
  select(-Partisanship) %>% 
  filter(!is.na(selected))

df.democrat.public <- df_cleaned_public %>% 
  filter(Partisanship == "Democrat") %>% 
  select(-Partisanship) %>% 
  filter(!is.na(selected))

# Republicans evaluating Democrat vs. Republican candidates (Donors)

rep.rep.donors <- df.republican.donors %>% filter(partisanship_dum == 1) 
rep.dem.donors <- df.democrat.donors %>% filter(partisanship_dum == 1)

rep.rep.donors <- estimate_AMCEs("Republicans evaluating Republicans (Donors, March 2019)", rep.rep.donors, model)
rep.dem.donors <- estimate_AMCEs("Republicans evaluating Democrats (Donors, March 2019)", rep.dem.donors, model)

comparison1 <- compare_AMCEs_2(rep.rep.donors, rep.dem.donors, "Republican candidates", "Democratic candidates")
plot_AMCEs_comparison("Republicans evaluate Republican vs. Democratic candidates (donors)", comparison1)

# Democrats evaluating Democrat vs. Republican candidates (Donors)

dem.rep.donors <- df.republican.donors %>% filter(partisanship_dum == 2) 
dem.dem.donors <- df.democrat.donors %>% filter(partisanship_dum == 2)

dem.rep.donors <- estimate_AMCEs("Democrats evaluating Republicans (Donors, March 2019)", dem.rep.donors, model)
dem.dem.donors <- estimate_AMCEs("Democrats evaluating Democrats (Donors, March 2019)", dem.dem.donors, model)

comparison2 <- compare_AMCEs_2(dem.rep.donors, dem.dem.donors, "Republican candidates", "Democratic candidates")
plot_AMCEs_comparison("Democrats evaluate Republican vs. Democratic candidates (donors)", comparison2)

# Republicans evaluating Democrat vs. Republican candidates (Public)

rep.rep.public <- df.republican.public %>% filter(partisanship_dum_7 == 1) 
rep.dem.public <- df.democrat.public %>% filter(partisanship_dum_7 == 1)

rep.rep.public <- estimate_AMCEs("Republicans evaluating Republicans (Public, March 2019)", rep.rep.public, model)
rep.dem.public <- estimate_AMCEs("Republicans evaluating Democrats (Public, March 2019)", rep.dem.public, model)

comparison3 <- compare_AMCEs_2(rep.rep.public, rep.dem.public, "Republican candidates", "Democratic candidates")
plot_AMCEs_comparison("Republicans evaluate Republican vs. Democratic candidates (public)", comparison3)

# Democrats evaluating Democrat vs. Republican candidates (Public)

dem.rep.public <- df.republican.public %>% filter(partisanship_dum_7 == 2) 
dem.dem.public <- df.democrat.public %>% filter(partisanship_dum_7 == 2)

dem.rep.public <- estimate_AMCEs("Democrats evaluating Republicans (Public, March 2019)", dem.rep.public, model)
dem.dem.public <- estimate_AMCEs("Democrats evaluating Democrats (Public, March 2019)", dem.dem.public, model)

comparison4 <- compare_AMCEs_2(dem.rep.public, dem.dem.public, "Republican candidates", "Democratic candidates")
plot_AMCEs_comparison("Democrats evaluate Republican vs. Democratic candidates (public)", comparison4)
